﻿Imports System.Windows.Forms

Partial Public Class DatabaseGridControl

    Private WithEvents m_dgv As DataGridView = Nothing

    Private Sub DatabaseGridControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Loaded
        If System.ComponentModel.DesignerProperties.GetIsInDesignMode(Me) Then Return

        m_dgv = WindowsFormsHost1.Child
        m_dgv.AllowUserToAddRows = False

        AddHandler g_album_mgr.AlbumLoaded, AddressOf OnAlbumLoaded
        AddHandler g_connection.ConnectionChanged, AddressOf OnConnectionChanged
        AddHandler g_currentphoto.Changed, AddressOf OnPhotoChanged
    End Sub

    Protected Overrides Function MeasureOverride(ByVal constraint As System.Windows.Size) As System.Windows.Size
        If Double.IsInfinity(constraint.Width) Then Return New Size(100, 100)
        Return constraint
    End Function

    Private Delegate Sub DlgOnConnectionChanged()
    Private Sub OnConnectionChanged()
        If Not Me.Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgOnConnectionChanged(AddressOf OnConnectionChanged))
        Else
            DataGridUtils.ClearGrid(m_dgv)
        End If
    End Sub

    Private Sub OnAlbumLoaded()
        UpdateGrid()
    End Sub

    Private Delegate Sub DlgUpdateGrid()
    Private Sub UpdateGrid()
        If Not Me.Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgUpdateGrid(AddressOf UpdateGrid))
        Else
            DataGridUtils.UpdateGrid(m_dgv, g_album_mgr.Photos, False)
        End If
    End Sub

    Private Sub dgvPhotos_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles m_dgv.CellFormatting
        Static nullstyles As New CellStyleDictionary
        DataGridUtils.CellFormatting(m_dgv, nullstyles, sender, e)
    End Sub

    Private Sub dgvPhotos_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles m_dgv.CellDoubleClick
        If Not SetPhoto(e.RowIndex) Then Return
        ImageViewerForm.ShowFullImage(g_currentphoto.Row)
    End Sub

    Private Sub dgvPhotos_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles m_dgv.RowEnter
        SetPhoto(e.RowIndex)
    End Sub

    Private Sub OnPhotoChanged()
        If g_currentphoto Is Nothing Then Return
        Dim row As DataRow = g_currentphoto.Row
        If row Is Nothing Then Return
        Dim rowguid As Guid = CType(row.Item("RowGuid"), Guid)
        If rowguid = Guid.Empty Then Return

        m_dgv.ClearSelection()
        Dim rowguidcol As DataGridViewColumn = m_dgv.Columns.Item("RowGuid")
        If rowguidcol Is Nothing Then Return
        For Each drow As DataGridViewRow In m_dgv.Rows
            Dim cell As Guid = CType(drow.Cells.Item(rowguidcol.Index).Value, Guid)
            If cell = rowguid Then
                drow.Selected = True
            End If
        Next
    End Sub


    Private Function SetPhoto(ByVal index As Integer) As Boolean
        If index < 0 Then Return False
        Dim dgvrow As DataGridViewRow = m_dgv.Rows(index)
        If dgvrow Is Nothing Then Return False
        Dim row As DataRow = CType(dgvrow.DataBoundItem, DataRowView).Row
        If row Is Nothing Then Return False
        g_currentphoto.Row = row
        Return True
    End Function

End Class